<?php

namespace App\Admin\Controllers;

use App\Models\Course;
use App\Models\CourseRecommend;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CourseRecommendController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '课程推荐';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new CourseRecommend());

        $grid->column('id', 'ID');
        $grid->column('course.name', '课程');
        $grid->column('reason', '推荐理由');
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(CourseRecommend::findOrFail($id));

        $show->field('id', 'ID');
        $show->field('course_id', '课程');
        $show->field('reason', '推荐理由');
        $show->field('created_at', '创建时间');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new CourseRecommend());
        $courses = Course::pluckInfo();

        $form->select('course_id', '课程')->options($courses)->rules(function ($form) {
            if ($id = $form->model()->id) {
                return 'nullable|unique:course_recommends,course_id,' . $id . ',id';
            } else {
                return 'nullable|unique:course_recommends,course_id';
            }
        });
        $form->text('reason', '推荐理由');

        return $form;
    }
}
